package xcmg.device.web.boxup;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.boxup.BoxupMainService;
import xcmg.device.service.vo.boxup.*;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.log.LogHelper;

import java.util.List;

/**
 * Created by sqq on 2018/2/27.
 */
@RestController
@RequestMapping("/boxup")
public class BoxupController extends BasicController {

    @Autowired
    private BoxupMainService boxupMainService;

    private static final Object obj = new Object();

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult add(@RequestBody BoxupParam boxupParam) throws Exception{
        LogHelper.monitor("调用/boxup/add新增装箱，param: " + JSON.toJSONString(boxupParam));
        boxupParam.setOrgId(getOrgId());
        try {
            ErrorCode code;
            //并发控制
            synchronized (obj){
                code=boxupMainService.add(boxupParam,getUserId(),getOrgId(),getUserName());
            }
            return actionResult(code);
        }catch (Exception e){
            return actionResult(ErrorCode.Failure,e.getMessage());
        }
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult update(@RequestBody BoxupMainVO boxupMainVO) throws Exception{
        try {
            LogHelper.debug("调用/boxup/update更新装箱记录，param: " + JSON.toJSONString(boxupMainVO));
            ErrorCode code=boxupMainService.update(boxupMainVO,getUserId(),getUserName(),getOrgId());
            return actionResult(code);
        }catch (Exception e){
            return actionResult(ErrorCode.Failure,e.getMessage());
        }
    }

    @RequestMapping(value = "/keepPacking", method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult keepPacking(@RequestBody KeepPackingParam packingParam) {
        try {
            ErrorCode code = boxupMainService.keepPacking(packingParam.getBoxupId(), packingParam.getBarcode(), getOrgId());
            return actionResult(code);
        } catch (Exception e) {
            return actionResult(ErrorCode.Failure, e.getMessage());
        }
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult list(BoxupMainVO boxupMainVO) throws Exception{
        if(boxupMainVO==null){
            boxupMainVO=new BoxupMainVO();
        }
        boxupMainVO.setOrgId(getOrgId());
        List<BoxupMainVO> results=boxupMainService.list(boxupMainVO);
        return actionValueResult(results);
    }

    @RequestMapping(value = "/detailList",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult detailList(String boxupId){
        List<BoxupDetailVO> results=boxupMainService.detailList(boxupId);
        return actionValueResult(results);
    }

    @RequestMapping(value = "/computeTotal",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult computeTotal(String deliveryReceiptNo){
        BoxupIndexVO boxupIndexVO =new BoxupIndexVO();
        ErrorCode code=boxupMainService.computeTotal(deliveryReceiptNo,boxupIndexVO);
        return actionResult(code,boxupIndexVO);
    }

    @RequestMapping(value = "/notPackList",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult notPackList(String deliveryReceiptNo){
        List<BoxupDetailVO> results=boxupMainService.notPackList(deliveryReceiptNo);
        return actionValueResult(results);
    }

    @RequestMapping(value = "/beenPackList",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult beenPackList(String deliveryReceiptNo){
        List<BoxupDetailVO> results=boxupMainService.beenPackList(deliveryReceiptNo);
        return actionValueResult(results);
    }

    @RequestMapping(value = "/materialsDevanning",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult materialsDevanning(@RequestBody MaterialsDevanningParam materialsDevanningParam)throws Exception{
        materialsDevanningParam.setOrgId(getOrgId());
        ErrorCode code=boxupMainService.materialsDevanning(materialsDevanningParam);
        return actionResult(code);
    }

    @RequestMapping(value = "/boxDevanning",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult boxDevanning(@RequestBody BoxupMainVO boxVO){
        ErrorCode code=boxupMainService.boxDevanning(boxVO.getBoxupId());
        return actionResult(code);
    }


    @RequestMapping(value = "/listByDelivery",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult listByDelivery(BoxupMainVO boxupMainVO) throws Exception {
        if(boxupMainVO==null){
            boxupMainVO=new BoxupMainVO();
        }
        boxupMainVO.setOrgId(getOrgId());

//        boxupMainVO.setOrgId("186089832693366784");
        List<BoxupMainVO> results=boxupMainService.listByDelivery(boxupMainVO);
        return actionValueResult(results);
    }
}
